import { defineStore } from 'pinia'
export const useStore = defineStore('main', {
  state() {
    return {
      userInfo: uni.getStorageSync('userInfo') && JSON.parse(uni.getStorageSync('userInfo')),
      org: uni.getStorageSync('orgInfo') && JSON.parse(uni.getStorageSync('orgInfo')),
      shop: uni.getStorageSync('shopInfo') && JSON.parse(uni.getStorageSync('shopInfo')),
      orgType: uni.getStorageSync('orgType') || '',
      orgField: {
        '10801': 'sjsId',
        '10802': 'bmId',
        '10803': 'sjzId',
        '10805': 'orgId'
      },
      token: uni.getStorageSync('token') || '',
    }
  },
  getters: {
    userId() {
      return this.userInfo?.id
    },
    username() {
      return this.userInfo?.name
    },
    token() {
      return this.token
    }
  },
  actions: {
    async getUserInfo() {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await http.post('/wxUser/getUserInfo', { openId: uni.getStorageSync('openid') })
          let userInfo = { ...res.data, ...res.data.attach, unionid: res.data.wxUser.unionid, nickname: res.data.wxUser.nickname, headimgurl: res.data.wxUser.headimgurl }
          uni.setStorageSync('openid', userInfo.openId)
          uni.setStorageSync('unionid', userInfo.unionid)
          delete userInfo.attach
          delete userInfo.wxUser
          this.userInfo = userInfo
          uni.setStorageSync('userInfo', JSON.stringify(this.userInfo))
          return resolve(res.data)
        } catch (error) {
          return reject(error)
        }
      })
    },
    async clear() {
      this.userInfo = {}
    },
    async getUserInfoByCode(code) {
      try {
        const res = await http.post('/wxUser/getOpenid', { code })
        let userInfo = { ...res.data, ...res.data.attach, unionid: res.data.wxUser.unionid, nickname: res.data.wxUser.nickname, headimgurl: res.data.wxUser.headimgurl }
        uni.setStorageSync('openid', userInfo.openId)
        uni.setStorageSync('unionid', userInfo.unionid)
        delete userInfo.attach
        delete userInfo.wxUser
        this.userInfo = userInfo
        uni.setStorageSync('userInfo', JSON.stringify(this.userInfo))
        return Promise.resolve(res.data)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async getOrgInfo() {
      const res = await http.post('/volunteerOrg/getOrgInfo')
      if (res.data.shop) {
        this.shop = res.data.shop
        uni.setStorageSync('shopInfo', JSON.stringify(this.shop))
      }
      if (res.data.org) {
        this.org = res.data.org
        this.org.cname = res.data?.user?.cname
        this.org.ename = res.data?.user?.ename
        this.orgType = res.data?.orgType
        this.org.isBindWechat = res.data?.isBindWechat
        uni.setStorageSync('orgInfo', JSON.stringify(this.org))
        uni.setStorageSync('orgType', this.orgType)
      }
      return Promise.resolve(res.data)
    },
    getParamsObj(route) {
      // const currentRoute = '/' + getCurrentPages()[getCurrentPages().length - 1].route
      // console.log(currentRoute, JSON.parse(uni.getStorageSync(currentRoute)))
      if (!uni.getStorageSync(route)) {
        return null
      }
      return JSON.parse(uni.getStorageSync(route))
    },
    setParamsObj(url, obj) {
      if (!obj) {
        uni.removeStorageSync(url)
      }
      uni.setStorageSync(url, JSON.stringify(obj))
      window.history.pushState(null, null, document.URL + '#1')
      uni.switchTab({ url: url })
    }
  }
})

// export const store = useStore()